import os

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from globals import Globals

class School(webapp.RequestHandler):
    def get(self):
        globals = Globals()
        
        if self.request.get('sch'):
            school = db.get(db.Key(self.request.get('sch')))
                        
            deptsTable = '<table>\n'
            for dept in school.departments:
                dName = dept.dName
                
                for course in dept.courses:                
                    deptsTable += ('<tr>\n<td><a href="/course?ckey=%s">%s</a></td>\n</tr>\n' % (course.key(), (dept.dAbbr + ' ' + str(course.cNo))))
            deptsTable += '</table>\n'
            deptsTable += ('<a href="/addcourse?sch=%s">Add Course</a>' % self.request.get('sch'))
                            
            template_values = {
                               'globals': globals,
                               'courseTable': deptsTable,
                               'schName': school.schName,
                               'schAbbr': school.schAbbr,
                               'webUrl': school.webUrl,
                               'emailSfx': school.emailSfx}
            
            path = os.path.join(os.path.dirname(__file__), 'templates/school.html')
            self.response.out.write(template.render(path, template_values))
        else: 
            self.redirect('/')
